﻿using System.Data;
using System.Diagnostics.CodeAnalysis;
using System.Text;



namespace 弱智小游戏
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int w = 50;
            int h = 30;
            ConsoleInit(w, h);
            //申明一个 表示场景标识的变量
            E_ScenceType nowScenceType = E_ScenceType.begin;
            while (true)
            {
                switch (nowScenceType)
                {
                    //开始界面逻辑
                    case E_ScenceType.begin:
                        Console.Clear();//每次进入新的界面清除上一个界面
                        BeginScence(w,h,ref nowScenceType);
                        break;
                    //游戏界面逻辑
                    case E_ScenceType.game:
                        Console.Clear();
                        GameScence(w,h);
                        break;
                    //结束界面逻辑
                    case E_ScenceType.end:
                        Console.Clear();
                        break;
                }
            }
        }
        static void ConsoleInit(int w, int h)
        {
            //光标隐藏
            Console.CursorVisible = false;
            //舞台大小
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w, h);
        }
        static void BeginScence(int w,int h,ref E_ScenceType nowScenceType)
        {
            Console.SetCursorPosition(w / 2 - 5, 8);
            Console.Write("弱智小游戏");
            //当前选项编号
            int nowScenceIndex = 0;
            //
            bool isQuitBegin = false;
            while (true)
            {
                isQuitBegin = false ;
                Console.SetCursorPosition(w / 2 - 4,14);
                Console.ForegroundColor  = nowScenceIndex == 0 ? ConsoleColor.Magenta : ConsoleColor.White;
                Console.Write("开始游戏");
                Console.SetCursorPosition(w / 2 - 4,15);
                Console.ForegroundColor = nowScenceIndex == 1 ? ConsoleColor.Magenta : ConsoleColor.White;
                Console.Write("退出游戏");

                //通过RadKey可以得到一个输入类型的枚举类型（电脑自带的）
                switch (Console.ReadKey(true).Key)//注意不要回车，回车会出现一大堆，挑自己需要的就好
                {
                    case ConsoleKey.W:
                        --nowScenceIndex;
                        if (nowScenceIndex < 0)
                        {
                            nowScenceIndex = 0;
                        }
                        break;
                    case ConsoleKey.S:
                        ++nowScenceIndex;
                        if (nowScenceIndex > 1)
                        {
                            nowScenceIndex = 1;
                        }
                        break;
                    case ConsoleKey.J:
                        if (nowScenceIndex == 0)
                        {
                            //开始游戏
                            //1.改变当前场景ID
                            nowScenceType = E_ScenceType.game;
                            //2.退出当前循环(如果只有break，会直接返回到switch重来)
                            isQuitBegin = true;
                        }
                        else
                        {
                            //结束游戏
                        }
                            break;
                }
                //通过标识决定 是否跳出 开始场景的循环
                if (isQuitBegin)
                {
                    break;
                }
            }
        }
        static void GameScence(int w,int h)
        {
            //绘制不变的基本信息（单独一函数块）
            DrawWall(w, h);
            //绘制地图
            Map map = new Map(14,3,90);
            map.Draw();
            //绘制玩家

            //游戏场景循环
            while (true)
            {
                //游戏逻辑
            }
        }
        static void DrawWall(int w,int h)//墙和文字信息
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.ForegroundColor= ConsoleColor.Red;
            //画墙
            //竖
            //横着的
            for (int i = 0; i < w; i += 2)
            {
                //最上方墙
                Console.SetCursorPosition(i, 0);
                Console.Write("■");
                //最下方墙
                Console.SetCursorPosition(i, h - 1);
                Console.Write("■");
                //中间上的墙
                Console.SetCursorPosition(i, h - 11);
                Console.Write("■");
                //中间下的墙
                Console.SetCursorPosition(i, h - 6);
                Console.Write("■");
            }
            //竖着的
            for (int i = 0;i < h; i++)
            {
                Console.SetCursorPosition(0, i);
                Console.Write("■");
                Console.SetCursorPosition(w - 2, i);
                Console.Write("■");
            }
            //文字信息
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(2,h - 10);
            Console.Write("▢ ：普通格子");

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.SetCursorPosition(2, h - 9);
            Console.Write("▣：暂停一回合不动");
            Console.Write("   ");
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("◒ ：炸弹，倒退五格");

            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(2, h - 8);
            Console.Write("↹：随机倒退，暂停，换位置");

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.SetCursorPosition(2, h - 7);
            Console.Write("◆：玩家");
            Console.Write("  ");
            Console.ForegroundColor = ConsoleColor.DarkCyan;
            Console.Write("▼：电脑");
            Console.Write("  ");
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("◍ ：玩家电脑重合");

            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(2, h - 5);
            Console.Write("按任意键开始掷色子");
        }
    }
    //场景枚举
    enum E_ScenceType
    {
        begin,
        game,
        end, 
    }
    //格子枚举
    enum E_Grid_Type
    {
        //普通格子
        Normal,
        //炸弹
        boom,
        //暂停
        pause,
        //随机倒退、暂停、换位置
        Tunnel,
    }
    struct Grid
    {
        //格子类型
        public E_Grid_Type type;
        //格子位置
        public Vector2 pos;
        //初始化构造函数
        public Grid(int x,int y,E_Grid_Type type)
        {
            this.type = type;
            pos.x = x; 
            pos.y = y;
        }
        public void Draw()
        {
            //提出来的目的是 少写几行代码 不管哪种类型 都要先有位置在画
            Console.SetCursorPosition(pos.x,pos.y);
            switch (type)
            {
                case E_Grid_Type.Normal:
                    Console.ForegroundColor= ConsoleColor.White;
                    Console.SetCursorPosition(pos.x,pos.y);
                    Console.Write("▢");
                    break;
                case E_Grid_Type.boom:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.SetCursorPosition(pos.x, pos.y);
                    Console.Write("◒");
                    break;
                case E_Grid_Type.pause:
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.SetCursorPosition(pos.x, pos.y);
                    Console.Write("▣");
                    break;
                case E_Grid_Type.Tunnel:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.SetCursorPosition(pos.x, pos.y);
                    Console.Write("↹");
                    break;
                default:
                    break;
            }

        }
    }
    //位置信息结构体 包含xy位置
    struct Vector2
    {
        public int x;
        public int y;
        public Vector2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    struct Map
    {
        public Grid[] Grids;
        public Map(int x, int y, int num)
        {
            Grids = new Grid[num];

            //用于改变计数的变量
            //表示x、y变化的次数
            int indexX = 0;
            int indexY = 0;

            //x的步长
            int stepNum = 2;

            int RandomNum;
            Random r = new Random();
            for (int i = 0; i < num; i++)
            {
                //应该初始化格子类型
                RandomNum = r.Next(0, 101);
                //设置类型
                //80%几率是普通格子（首尾两个格子必须是普通格子）
                if (RandomNum < 80 || i == 0 || i == num - 1)
                {
                    Grids[i].type = E_Grid_Type.Normal;
                }
                else if (RandomNum >= 80 && RandomNum < 87)
                {
                    Grids[i].type = E_Grid_Type.boom;
                }
                else if (RandomNum >= 87 && RandomNum < 92)
                {
                    Grids[i].type = E_Grid_Type.Tunnel;
                }
                else
                {
                    Grids[i].type = E_Grid_Type.pause;
                }

                //位置应该如何设置
                Grids[i].pos = new Vector2(x, y);
                //每次循环都应该按一定的规则去变化位置
                //加十次
                if (indexX == 10)
                {
                    y += 1;
                    //加一次y记一次数
                    ++indexY;
                    if (indexY == 2)
                    {
                        //y加了两次之后 把x加的次数记0
                        indexX = 0;
                        indexY = 0;

                        //反向步长（然后就能呈现出弯曲状了）
                        stepNum = -stepNum;
                    }
                }
                else
                {
                    x += stepNum;
                    //加一次x记一次数
                    ++indexX;
                }
            }
        }
        public void Draw()
        {
            //遍历达到画格子的目的
            for (int i = 0; i < Grids.Length; ++i)
            {
                Grids[i].Draw();
            }
        }
    }
}
